/*
 * KKRenderTexture.cpp
 *
 *  Created on: 2013-9-23
 *      Author: wanghua
 */

#include "KKRenderTexture.h"

using namespace std;

USING_NS_CC;

KKRenderTexture::KKRenderTexture() {
	m_brush = NULL;
	m_pointCount =0 ;
}

KKRenderTexture::~KKRenderTexture() {
	CC_SAFE_RELEASE(m_brush);
}

KKRenderTexture * KKRenderTexture::renderTextureWithTarget(KKNode* target,
		const char* brushfile) {
	KKRenderTexture* render = new KKRenderTexture();

	if (render->initWithWidthAndHeight(target->getWidth(), target->getHeight(),kTexture2DPixelFormat_RGBA4444)) {
		render->autorelease();
		render->retain();

		CCNode* renderSprite = dynamic_cast<CCNode*> (target);

		render->setPosition(target->getWidth() / 2, target->getHeight() / 2);

		renderSprite->addChild(render, 100, 100);

		CCSprite* brush = CCSprite::create(brushfile);



		if (brush) {
			brush->retain();
			render->setBrush(brush);
		}

		return render;
	}

	CC_SAFE_DELETE(render);

	return NULL;

}
void KKRenderTexture::addPoint(CCPoint end, CCPoint dir) {

	m_pointCount++;

	CCPoint start = ccp(end.x-dir.x,end.y-dir.y);

	this->begin();

	float distance = ccpLength(dir);

	if (distance > 1) {
		int d = (int) distance;
		for (int i = 0; i < d; i++) {
			float difx = end.x - start.x;
			float dify = end.y - start.y;
			float delta = (float) i / distance;
			m_brush->setPosition(
					ccp(start.x + (difx * delta), start.y + (dify * delta)));
			m_brush->visit();
		}
	}

	this->end();
}

